﻿using Assistant;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YoucaiReader.Entities;

/**
 * 创建时间：2014/9/20 12:58:06
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace YoucaiReader.Helper
{
    public class SaveReaderHistory
    {
        public static void Delete(BookListModel blm)
        {
            BookListModel valueFromSettings = new BookListModel();
            BookListModel t_m2 = new BookListModel(), t_m3 = new BookListModel(), t_m4 = new BookListModel();
            if (SettingHelper.IsSettings("ReaderHistory1"))
            {
                valueFromSettings = SettingHelper.GetValueFromSettings("ReaderHistory1") as BookListModel;
            }
            if (SettingHelper.IsSettings("ReaderHistory2"))
            {
                t_m2 = SettingHelper.GetValueFromSettings("ReaderHistory2") as BookListModel;
            }
            if (SettingHelper.IsSettings("ReaderHistory3"))
            {
                t_m3 = SettingHelper.GetValueFromSettings("ReaderHistory3") as BookListModel;
            }
            if (SettingHelper.IsSettings("ReaderHistory4"))
            {
                t_m4 = SettingHelper.GetValueFromSettings("ReaderHistory4") as BookListModel;
            }
            //
            if (t_m4.str_Id_M == blm.str_Id_M)
            {
                SettingHelper.DeleteSettings("ReaderHistory4");
            }
            else if (t_m3.str_Id_M == blm.str_Id_M)
            {
                SettingHelper.DeleteSettings("ReaderHistory3");
                if (SettingHelper.IsSettings("ReaderHistory4"))
                {
                    SettingHelper.SaveInToSettings("ReaderHistory3", t_m4);
                    SettingHelper.DeleteSettings("ReaderHistory4");
                }
            }
            else if (t_m2.str_Id_M == blm.str_Id_M)
            {
                SettingHelper.DeleteSettings("ReaderHistory2");
                if (SettingHelper.IsSettings("ReaderHistory3"))
                {
                    SettingHelper.SaveInToSettings("ReaderHistory2", t_m3);
                    SettingHelper.DeleteSettings("ReaderHistory3");
                }
            }
            else if (valueFromSettings.str_Id_M == blm.str_Id_M)
            {
                SettingHelper.DeleteSettings("ReaderHistory1");
                if (SettingHelper.IsSettings("ReaderHistory4"))
                {
                    SettingHelper.SaveInToSettings("ReaderHistory1", t_m2);
                    SettingHelper.SaveInToSettings("ReaderHistory2", t_m3);
                    SettingHelper.SaveInToSettings("ReaderHistory3", t_m4);
                    SettingHelper.DeleteSettings("ReaderHistory4");
                }
                else if (SettingHelper.IsSettings("ReaderHistory3"))
                {
                    SettingHelper.SaveInToSettings("ReaderHistory1", t_m2);
                    SettingHelper.SaveInToSettings("ReaderHistory2", t_m3);
                    SettingHelper.DeleteSettings("ReaderHistory3");
                }
                else if (SettingHelper.IsSettings("ReaderHistory2"))
                {
                    SettingHelper.SaveInToSettings("ReaderHistory1", t_m2);
                    SettingHelper.DeleteSettings("ReaderHistory2");
                }
            }


            //if (t_m3.str_Id_M == blm.str_Id_M)
            //{
            //    SettingHelper.DeleteSettings("ReaderHistory3");
            //}
            //else if (t_m2.str_Id_M == blm.str_Id_M)
            //{
            //    SettingHelper.DeleteSettings("ReaderHistory2");
            //    if (SettingHelper.IsSettings("ReaderHistory3"))
            //    {
            //        SettingHelper.SaveInToSettings("ReaderHistory2", t_m3);
            //        SettingHelper.DeleteSettings("ReaderHistory3");
            //    }
            //}
            //else if (valueFromSettings.str_Id_M == blm.str_Id_M)
            //{
            //    SettingHelper.DeleteSettings("ReaderHistory1");
            //    if (SettingHelper.IsSettings("ReaderHistory3"))
            //    {
            //        SettingHelper.SaveInToSettings("ReaderHistory1", t_m2);
            //        SettingHelper.SaveInToSettings("ReaderHistory2", t_m3);
            //        SettingHelper.DeleteSettings("ReaderHistory3");
            //    }
            //    else if (SettingHelper.IsSettings("ReaderHistory2"))
            //    {
            //        SettingHelper.SaveInToSettings("ReaderHistory1", t_m2);
            //        SettingHelper.DeleteSettings("ReaderHistory2");
            //    }
            //}
        }

        public static void Save(BookListModel blm)
        {
            Delete(blm);
            if (SettingHelper.IsSettings("ReaderHistory3"))
            {
                BookListModel valueFromSettings = new BookListModel();
                valueFromSettings = SettingHelper.GetValueFromSettings("ReaderHistory3") as BookListModel;
                SettingHelper.SaveInToSettings("ReaderHistory4", valueFromSettings);
            }
            if (SettingHelper.IsSettings("ReaderHistory2"))
            {
                BookListModel valueFromSettings = new BookListModel();
                valueFromSettings = SettingHelper.GetValueFromSettings("ReaderHistory2") as BookListModel;
                SettingHelper.SaveInToSettings("ReaderHistory3", valueFromSettings);
            }
            if (SettingHelper.IsSettings("ReaderHistory1"))
            {
                BookListModel valueFromSettings = new BookListModel();
                valueFromSettings = SettingHelper.GetValueFromSettings("ReaderHistory1") as BookListModel;
                SettingHelper.SaveInToSettings("ReaderHistory2", valueFromSettings);
            }
            SettingHelper.SaveInToSettings("ReaderHistory1", blm);
        }
    }
}
